package main

import "fmt"

// 写数据
func writeData(ch chan int) {
	for i := 0; i < 50; i++ {
		ch <- i
		fmt.Println("writeData intChan: ", i)
	}
	close(ch)
}

// 读数据，和两个chan 交互
func readData(ch1 chan int, ch2 chan bool) {
	for {
		v, ok := <-ch1
		if !ok {
			break
		}
		fmt.Println("readData intChan: ", v)
	}
	// 关闭boolChan
	ch2 <- true
	close(ch2)
}

func main() {
	// 声明两个管道
	intChan := make(chan int, 20)
	boolChan := make(chan bool, 1)
	go writeData(intChan)
	go readData(intChan, boolChan)
	// 阻塞主线程，监听退出信号，实现跟waitgroup相同的功能
	for {
		_, ok := <-boolChan
		if !ok {
			break
		}
	}
}
